<template>
  <el-dialog
    v-loading="loading"
    :visible.sync="dialog"
    :title="'确认提交选择的 ' + itemSize + ' 条item信息?'"
    :close-on-click-modal="false"
    width="1100px"
    append-to-body
    @close="cancel"
  >
    <div>
      <el-form ref="bpmsForm" :inline="true" :model="bpmsParam" :rules="rules" size="small" label-width="180px">
        <el-form-item label="初级审批人ID：" prop="auditBuyerId">
          <div style="width: 280px;"><span>{{ bpmsParam.auditBuyerId }}</span></div>
        </el-form-item>
        <el-form-item v-if="changeType === 1 || changeType === 3 || changeType === 6" label="陈列专员ID：" prop="moularId">
          <el-input v-model="bpmsParam.moularId" style="width: 280px;" />
        </el-form-item>
        <el-form-item v-if="changeType === 1 || changeType === 2 || changeType === 3 || changeType === 6" label="补货经理ID：" prop="replId">
          <el-input v-model="bpmsParam.replId" style="width: 280px;" />
        </el-form-item>
        <el-form-item v-if="changeType === 1 || changeType === 3 || changeType === 6" label="直接进口采购ID：" prop="dibuyerId">
          <el-input v-model="bpmsParam.dibuyerId" :placeholder="changeType === 1 ? '仅供邮件通知' : ''" style="width: 280px;" />
        </el-form-item>
        <el-form-item v-if="changeType === 1 || changeType === 3 || changeType === 6" label="自有品牌采购ID：" prop="cateBuyerId">
          <el-input v-model="bpmsParam.cateBuyerId" :placeholder="changeType === 1 ? '仅供邮件通知' : ''" style="width: 280px;" />
        </el-form-item>
        <el-form-item v-if="changeType === 1 || changeType === 2 || changeType === 3 || changeType === 6" label="采购共享团队：" prop="shareHub">
          <el-select v-model="bpmsParam.shareHub" class="filter-item" style="width: 280px">
            <el-option label="China Produce Share Hub" value="CND94SH@walmart.com" />
            <el-option label="China Seafood and Meat Share Hub" value="CNSAMSH@walmart.com" />
            <el-option label="China Grocery Share Hub D01/82/96" value="CNSH018296@walmart.com" />
            <el-option label="China Fresh Deli And Bakery Share Hub" value="CNFDABSH@walmart.com" />
            <el-option label="China Electronics Share Hub Team" value="ChinaESHT@walmart.com" />
            <el-option label="China Apparel Share Hub Team" value="CNSLSHT@walmart.com" />
            <el-option label="China Share Hub Home Hardlines" value="cnsharehubhh@walmart.com" />
            <el-option label="China Consumables Share Hub" value="CNHCSHUB@walmart.com" />
            <el-option label="China D92-Share Hub Team" value="CND92ShareHub@walmart.com" />
            <el-option label="China DairynFrozen Share Hub" value="CNBUYERST@walmart.com" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="changeType === 6" label="Super采购：" prop="superBuyer">
          <el-select v-model="bpmsParam.superBuyer" class="filter-item" style="width: 280px">
            <el-option label="David Wu" value="d0w01cy" />
            <el-option label="Julia Zhu" value="jzhu103" />
            <el-option label="Jilly Han" value="jhan3" />
            <el-option label="Glider Xu" value="g2xuu" />
            <el-option label="Joy Lin" value="j0l01wu" />
            <el-option label="Jenny Tang" value="j3tang" />
            <el-option label="Sissi Huang" value="shuan25" />
            <el-option label="Lily Zhu" value="lzhu4" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="changeType === 6" label="Super补货：" prop="superRepl">
          <el-select v-model="bpmsParam.superRepl" class="filter-item" style="width: 280px">
            <el-option label="Wooden Liu" value="wliu04" />
            <el-option label="Crimson Lu" value="clu78" />
            <el-option label="Andy Li" value="a0l01tz" />
            <el-option label="Carol Wu" value="s0w04be" />
            <el-option label="Jacky Huang" value="jhuan35" />
            <el-option label="Alan Lu" value="alu51" />
            <el-option label="Leo Li" value="l0l01rn" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否加急：">
          <el-checkbox v-model="urgentChecked" size="small" />
          <span style="color:red;">注意：加急审批会提高审批级别！</span>
        </el-form-item>
        <el-form-item label="备注：" prop="remark">
          <el-input v-model="bpmsParam.remark" rows="5" type="textarea" style="width: 280px;" />
        </el-form-item>
        <el-form-item v-if="changeType === 6" label="Super采购意见：" prop="remark">
          <el-input v-model="bpmsParam.remarkSuperBuyer" rows="5" type="textarea" style="width: 280px;" />
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button type="primary" @click="submitBPMS">提交</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getUser } from '@/api/api'
import { submitBPMS } from '@/api/change-item'
import { Message } from 'element-ui'

export default {
  props: {
    parent: {
      type: Object,
      default: null
    }
  },
  data() {
    var validateUser = (rule, value, callback) => {
      if (value) {
        getUser(value).then(user => {
          if (!user) {
            callback(new Error('无效的USER ID'))
          } else {
            callback()
          }
        })
      } else {
        callback()
      }
    }
    return {
      loading: false,
      dialog: false,
      itemSize: 0,
      urgentChecked: false,
      bpmsParam: {},
      ids: [],
      changeType: 0,
      rules: {
        moularId: [
          { validator: validateUser, trigger: 'blur' }
        ],
        replId: [
          { validator: validateUser, trigger: 'blur' }
        ],
        dibuyerId: [
          { validator: validateUser, trigger: 'blur' }
        ],
        cateBuyerId: [
          { validator: validateUser, trigger: 'blur' }
        ],
        auditBuyerId: [
          { validator: validateUser, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.dialog = false
      this.urgentChecked = false
    },
    // 提交审核操作
    submitBPMS() {
      this.$refs.bpmsForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.bpmsParam.urgentFlag = this.urgentChecked ? 'Y' : 'N'
          submitBPMS({ changeType: this.changeType, bpmsParamDTO: this.bpmsParam, ids: this.ids }).then(res => {
            this.parent.toQuery()
            Message({
              message: '数据提交成功。',
              type: 'success',
              duration: 3 * 1000
            })
            this.loading = false
            this.dialog = false
          }, (error) => {
            console.log(error)
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

